﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class SkuClassBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOwnerDA OwnerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISkuClassDA skuClassDA = DataAccess.CreateSkuClassDA();

        public SkuClassBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (string.IsNullOrEmpty(currentWH))
            {
                throw new Exception("用户当前仓库ID为空");
            }
            this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
            if (this.dataBaseID == null)
            {
                throw new Exception("没有与当前仓库ID相对应的记录");
            }
        }

        public SkuClassBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                throw new Exception("用户当前仓库ID为空");
            }
            this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
            if (this.dataBaseID == null)
            {
                throw new Exception("没有与当前仓库ID相对应的记录");
            }
            this.pubParasInfo = new PubParasInfo();
            this.pubParasInfo = tempPubParasInfo;
        }

        public string DeleteDetail(List<string> skuClassID, List<string> OwenrID, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<SkuClassInfo> skuClassInfos = new List<SkuClassInfo>();
                SkuClassInfo skuClassInfo = null;
                for (i = 0; i < skuClassID.Count; i++)
                {
                    skuClassInfo = skuClassDA.GetSkuClassByID(dataBase, tran, skuClassID[i], OwenrID[i], this.dataBaseID[0]);
                    skuClassInfos.Add(skuClassInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (skuClassDA.DeleteSkuClass(dataBase, tran, skuClassID, OwenrID, this.dataBaseID) > 0)
                {
                    for (i = 0; i < skuClassID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<SkuClassInfo>(dataBase, tran, skuClassInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "SKU_CLASS", this.dataBaseID);
                        temp = temp + skuClassID[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllDetailInfo()
        {
            return skuClassDA.GetAllSkuClass(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            SkuClassQueryEntity skuClassQuery = obj as SkuClassQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(skuClassQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + skuClassQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(skuClassQuery.SkuClassID))
            {
                sqlWhere.Add(" SKU_CLASS_ID " + skuClassQuery.SkuClassID);
            }
            if (!string.IsNullOrEmpty(skuClassQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + skuClassQuery.Descr);
            }
            if (!string.IsNullOrEmpty(skuClassQuery.Descr2))
            {
                sqlWhere.Add(" DESCR2 " + skuClassQuery.Descr2);
            }
            if (!string.IsNullOrEmpty(skuClassQuery.IsActive))
            {
                sqlWhere.Add("  IS_ACTIVE " + skuClassQuery.IsActive);
            }
            return skuClassDA.GetSkuClassByQueryList(sqlWhere, skuClassQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string skuclassID, string OwnerID, string pk3)
        {
            return skuClassDA.GetSkuClassByID(skuclassID, OwnerID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException();
        }

        public string InsertDetail(object obj)
        {
            string skuClassID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SkuClassInfo skuClass = obj as SkuClassInfo;
                skuClass.UpdatedBy = skuClass.CreatedBy;
                skuClass.CreatedDate = DateTime.Now;
                skuClass.UpdatedDate = DateTime.Now;
                if (skuClassDA.CheckSkuClassIDUnique(dataBase, tran, skuClass.SkuClassID, skuClass.OwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2210_014", this.pubParasInfo.UserLanguage, new List<string> { skuClass.SkuClassID, skuClass.OwnerID });
                }
                if (OwnerDA.GetTradePartnerByID(dataBase, tran, skuClass.OwnerID, this.dataBaseID[0]) == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2270_002", this.pubParasInfo.UserLanguage, new List<string> { skuClass.OwnerID });
                }
                if (skuClassDA.InsertSkuClass(dataBase, tran, skuClass, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SkuClassInfo>(dataBase, tran, null, skuClass, AuditAction.Insert, this.pubParasInfo.UserID, "SKU_CLASS", this.dataBaseID);
                    skuClassID = skuClass.SkuClassID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return skuClassID;
        }

        public string UpdateDetail(object obj)
        {
            string skuClassID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SkuClassInfo skuClass = obj as SkuClassInfo;
                SkuClassInfo oldSkuClassInfo = skuClassDA.GetSkuClassByID(dataBase, tran, skuClass.SkuClassID, skuClass.OwnerID, this.dataBaseID[0]);
                if (skuClassDA.UpdateSkuClass(dataBase, tran, skuClass, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SkuClassInfo>(dataBase, tran, oldSkuClassInfo, skuClass, AuditAction.Update, this.pubParasInfo.UserID, "SKU_CLASS", this.dataBaseID);
                    skuClassID = skuClass.SkuClassID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return skuClassID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

